<?php

class RewardController extends AdminController {

    public $db;
    public function actionView() {
        $sort = new CSort;
        $sort->attributes = array(
            'reward_node_right' => array(
                'asc' => 'reward_node_right',
                'desc' => 'reward_node_right DESC',
            ),
            'reward_node_left' => array(
                'asc' => 'reward_node_left',
                'desc' => 'reward_node_left DESC',
            ),
            'network_mid' => array(
                'asc' => 'network_mid',
                'desc' => 'network_mid DESC',
            ),
        );


        $data_arr = array(
            'select' => '*',
            'from' => 'sys_reward sr',
            'join' => array(
                'sn' => 'sys_network sn',
                'sm' => 'sys_member sm',
            ),
            'params_join' => array(
                'sn' => 'sr.reward_network_id=sn.network_id',
                'sm' => 'sn.network_id=sm.member_network_id',
            ),
            'order' => 'network_id',
        );

        $results = $this->get_data($data_arr);
        $dataProvider = new CArrayDataProvider($results,
                        array(
                            'keyField' => 'network_id',
                            'sort' => $sort,
                ));
        
        $this->render('view', array(
            'dataProvider' => $dataProvider,
        ));
    }

    private function get_data($data_arr) {
        if (!isset($this->db)) {
            $this->db = Yii::app()->db->createCommand();
        }
        $this->db->select($data_arr['select']);
        $this->db->from($data_arr['from']);
        if ($data_arr['where'] != '') {
            $this->db->where($data_arr['where'], $data_arr['params']);
        }
        //  array('not in','network_level_status_log_value','passive'),
        // array('>=','date(network_level_status_log_datetime)',$start_daily)
        if (isset($data_arr['join'])) {
            foreach ($data_arr['join'] AS $key => $val) {

                $this->db->join($val, $data_arr['params_join'][$key]);
            }
        }

        if (isset($data_arr['group']) AND trim($data_arr['group']) != '') {
            $this->db->group($data_arr['group']);
        }
        if (isset($data_arr['order']) AND trim($data_arr['order']) != '') {
            $this->db->order($data_arr['order']);
        }

        return $this->db->queryAll();
    }

}

?>
